<%= include ic/admin-header.html %>
<style>
#data-answer{color:#e11;}
.answer{margin-bottom:10px;}
</style>
<div class="am-cf admin-main">
  <%= include ic/admin-sidebar.html %>

  <!-- content start -->
  <div class="admin-content">
    <div class="am-cf am-padding">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">题目管理</strong> / <small>编辑</small></div>
    </div>

	<div class="am-padding-left"><% if(result){ %><%= result.get("title") %><% } %></div>
    <hr style=" margin-top:0.5rem;" />
	<div class="am-g-fixed am-cf am-padding-left am-padding-bottom">
		<form class="am-form am-u-sm-12 am-form-horizontal am-cf" id="my-form">
			<% if(result){ %><input id="data-id" type="hidden" value="<%= result.id %>"><% } %>
			<div class="am-u-sm-12">
				<div class="am-form-group">
					<label for="data-title" class="am-u-sm-2 am-form-label">标题</label>
					<div class="am-u-sm-10">
						<input type="text" id="data-title" value="<% if(result){ %><%= result.get("title") %><% } %>" data-foolish-msg="2-144字符的题目" minlength="2" required>
					</div>
				</div>

				<div class="am-form-group am-form-inline">
					<label for="data-answer" class="am-u-sm-2 am-form-label">正确答案</label>
					<div class="am-u-sm-3">
            <% for(var i=0;i<4;i++){ %>
            <% if(answers[i].a){ %>
            <input type="text" id="data-answer" value="<%= answers[i].v %>" required>
            <% } %>
            <% } %>
					</div>
					<div class="am-u-sm-2"></div>
				</div>
        <div class="am-form-group am-form-inline">
					<label for="data-answer" class="am-u-sm-2 am-form-label">错误答案</label>
					<div class="am-u-sm-3" id="answers">
            <% for(var i=0;i<4;i++){ %>
            <% if(!answers[i].a){ %>
            <input type="text" class="answer" value="<%= answers[i].v %>" required>
            <% } %>
            <% } %>
					</div>
					<div class="am-u-sm-2"></div>
				</div>
			</div>
		</form>
		<hr class="am-u-sm-12 am-fl am-cf" />
		<div class="am-g am-u-sm-11 am-fl am-cf">
			<div class="am-u-sm-3">
				<div class="am-btn am-btn-default" onclick="history.go(-1)">返回</div>
			</div>
      <div class="am-u-sm-3">
				<div id="btn-saveEdit0" class="am-btn am-btn-default btn-saveEdit" onclick="history.go(0)">撤销</div>
			</div>
			<div class="am-u-sm-3 am-u-end">
				<div id="btn-saveEdit1" class="am-btn am-btn-warning btn-saveEdit" onclick="saveEdit('uncertain')">待定</div>
			</div>
      <div class="am-u-sm-3 am-u-end">
				<div id="btn-saveEdit1" class="am-btn am-btn-primary btn-saveEdit" onclick="saveEdit('passed')">确认保存</div>
			</div>
		</div>
		<div class="am-u-sm-12 am-fl am-cf" ></div>
	</div>
  </div>
  <!-- content end -->

</div>

<a class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu" data-am-offcanvas="{target: '#admin-offcanvas'}"></a>

<%= include ic/admin-footer.html %>

<script type="text/javascript">
var refer	= "detail";
var type  = "passed";
$(function() {
  $("input[type='text']").change( function() {
    type  = "edited";
  });
})
var validator 	= $('#my-form').validator({submit: function(e) {
	if(this.isFormValid()){
		$('#my-modal-loading').modal();
    var answers = $("#answers .answer").map(function(){
      return $(this).val();
    }).get().join("||");
    //console.log(answers);
		$.post("./save", {
			"ajax"			: true,
			"id"			  : $('#data-id').val(),
			"title"			: $('#data-title').val(),
			"answers"		: answers,
      "answer"    : $('#data-answer').val(),
			"type"			: type
		},function(data){
			$('#my-modal-loading').modal('close');
			if(typeof(data)=="object" && typeof(data.error)=="undefined"){
				mAlert({
					title:"提示",
					text:"保存成功！",
					onConfirm:function(e) {
						gotoUrl(refer);
					}
				});
			}else{
				//console.dir(data);
				mAlert({
					title:"错误",
					text:data.error,
					onConfirm:function(e) {}
				});
			}
		}, "json");
	}
	return false;
}});

function saveEdit(state){
  type=state;
	validator.submit();
}
</script>
